package cn.kgc.ssm.aspects;

import cn.hutool.json.JSONUtil;
import cn.kgc.ssm.annotations.LogInfos;
import cn.kgc.ssm.entity.LogInfo;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.mapper.LogInfoMapper;
import cn.kgc.ssm.utils.IpUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Method;

/**
 * @author 课工场
 * @date 2024/11/22
 * @description 日志通知类
 */
@Component
@Aspect
public class LogInfoAspect {


    @Autowired
    private LogInfoMapper logInfoMapper;

    @Pointcut("@annotation(cn.kgc.ssm.annotations.LogInfos)")
    public void point() {
    }

    // 环绕通知
    @Around("point()")
    public Object logInfoAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) {


        Long timeConsuming = null;
        Object result = null;
        try {
            //  耗时
            long start = System.currentTimeMillis();
            result = proceedingJoinPoint.proceed();
            long end = System.currentTimeMillis();
            timeConsuming = end - start;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }

        // 参数
        Object[] args = proceedingJoinPoint.getArgs();
        String params = JSONUtil.toJsonStr(args);


        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        LogInfos annotation = method.getAnnotation(LogInfos.class);
        // 操作类型   修改  删除  查询  增加
        String operation = annotation.operation().getInfo();
        // 模块   用户  会员
        String model = annotation.model();


        // 操作人    请求对象  request
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();

        // ip
        String ip = IpUtil.getIp(request);
        // 路径  url
        String requestURI = request.getRequestURI();


        // 封装数据
        LogInfo logInfo = new LogInfo();
        logInfo.setTimeConsuming(timeConsuming);
        logInfo.setIp(ip);
        logInfo.setUrl(requestURI);
        logInfo.setUsername(username);
        logInfo.setModel(model);
        logInfo.setParams(params);
        logInfo.setDescription(operation);


        // 添加数据到数据库
        logInfoMapper.addLogInfo(logInfo);


        return result;
    }


}
